package cvut.cz.via.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import cvut.cz.via.android.LoginActivity;
import cvut.cz.via.android.users.User;



public class InitializationActivity extends Activity {
    /** Called when the activity is first created. */
	protected String _filename = "config.dat";
	protected boolean _active = true;
	protected int _splashTime = 2000;  // time to display the splash screen in ms
	protected File _file;
	protected User _user;
	
    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.initialization_activity);
        startMainMenu();
    }*/
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
     
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                	// trying to open user file
                	FileInputStream fis;
					try {
						fis = openFileInput(_filename);
						Log.d("READING_FILE", "Reading_file");							
						ObjectInputStream ois = new ObjectInputStream(fis);
						_user = (User) ois.readObject();
						StudyMaterialsApplication sma = getStudyMaterialsApplication();
						sma.user = _user;
						finish(); 
						Intent intent = new Intent(InitializationActivity.this, MainMenuActivity.class);
	            		startActivity(intent);
	            		return;
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (StreamCorruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}					
					// login activity
					Log.d("START_LOGIN", "Starting_login");
            		finish();                		
            		Intent intent = new Intent(InitializationActivity.this, LoginActivity.class);
            		startActivity(intent);
                		
                	}
                }
        };
        splashTread.start();
    }

    /*@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }*/
    
	private void startMainMenu() {
		Intent intent = new Intent(InitializationActivity.this, LoginActivity.class);
		startActivity(intent);
	}
	
	private StudyMaterialsApplication getStudyMaterialsApplication() {
		StudyMaterialsApplication sma = (StudyMaterialsApplication)getApplication();
		return sma;
	}
	
	
}